package com.projectdemo.springbootdemo1.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @class: SpringMvcConfig
 * @author: zhangzw
 * @date: 2024-10-31
 * @description: SpringMvc配置类
 */
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

    /**
     * 增加页面控制类
     *
     * @param registry 视图控制注册表
     * @return void data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("testtest").setViewName("test");
    }

    /**
     * 注册我的视图解析器到容器中
     *
     * @return com.projectdemo.springbootdemo1.config.SpringMvcConfig.MyViewResolver data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @Bean
    public MyViewResolver getMyViewResolver(){
        return new MyViewResolver();
    }

    /**
     * 我的视图解析器类
     *
     * @date 2024/11/8
     * @author zhangzw
     **/
    public static class MyViewResolver implements ViewResolver {
        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }

    /**
     * 注册地区解析器到容器中
     *
     * @return org.springframework.web.servlet.LocaleResolver data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @Bean
    public LocaleResolver localeResolver(){
        return new MyLocaleResolver();
    }

    /**
     * 重写新增拦截器方法
     *
     * @param registry 拦截器注册表
     * @return void data
     * @date 2024/11/8
     * @author zhangzw
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        List<String> excludePath = new ArrayList<String>();
        excludePath.add("/");
        excludePath.add("/zh-CN/login");
        excludePath.add("/en-US/login");
        excludePath.add("/zh-CN/login.html");
        excludePath.add("/en-US/login.html");
        excludePath.add("/zh-CN/loginvalid");
        excludePath.add("/en-US/loginvalid");
        excludePath.add("/css/**");
        excludePath.add("/imgs/**");
        excludePath.add("/js/**");
        excludePath.add("/vendor/**");
        registry.addInterceptor(new LoginHandleInterceptor()).addPathPatterns("/**").excludePathPatterns(excludePath);
    }
}
